
/*
 * JUnit Test for Controller: saveData(), loadfile()
 * Author: Daniel Henderson
 * TCSS 360 Spring 2013 
 * University of Washington
 * May 19th 2013
 * 
 */
package JUnitTests;

import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;


import org.junit.Before;
import org.junit.Test;

import Model.Conference;
import Model.Controller;
import Model.Deadline;
import Model.User;

/**
 * Testing methods in the Controller class: saveData() loadFile() and various other methods to retrieve and set data for User List 
 * and the Conference List.
 * 
 * @author Daniel Henderson
 * @version UWT Spring 2013, May 18th. 
 * @group 3-squared-software-systems
 */
public class SerializeLoadSaveTest {

	private User admin;
	private User john; //pchair 1
	private User jack; //pchair 2
	private User jim;  //pchair 3
	private User james;	
	private User jill;	
	private User judy;	
	private User jake;
	private User janas;
	private User jimmy;
	private User jeremy;

	private String conf1Name;
	private String conf2Name;
	private String conf3Name;
	
	private Deadline deadline1;
	private Deadline deadline2;
	private Deadline deadline3;
	
	private Conference conf1;
	private Conference conf2;
	private Conference conf3;
	
	private Controller controller;
	
	private List<User> users;
	private List<Conference> conferences;
	
	/**
	 * Construct List of Conferences and Users to test.
	 * 
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		
		//create new Controller
		controller = new Controller();
		
		//initiate Lists
		users = new ArrayList<User>();
		conferences = new ArrayList<Conference>();
		
		//create default admin account
		admin = new User("admin", "password", "email@yahoo.com", "Default Admin", true);
		
		//Conference 1 Data
		john = new User("john1", "password", "john@yahoo.com", "john"); //pchair 1
		conf1Name = "Conference 1";
		deadline1 = new Deadline(5, 19, 2013);
		conf1 = new Conference(conf1Name, john, deadline1, deadline1, deadline1, deadline1, deadline1);
		
		//Conference 2 Data
		jack = new User("jack1", "password", "jack@yahoo.com", "jack");//pchair 2
		conf2Name = "Conference 2";
		deadline2 = new Deadline(5, 20, 2013);
		conf2 = new Conference(conf2Name, jack, deadline2, deadline2, deadline2, deadline2, deadline2);
		
		//Conference 3 Data
		jim = new User("jim1", "password", "jim@yahoo.com", "jim");//pchair 3
		conf3Name = "Conference 3";
		deadline3 = new Deadline(5, 21, 2013);
		conf3 = new Conference(conf3Name, jim, deadline3, deadline3, deadline3, deadline3, deadline3);
		
		//Normal Users
		james = new User("james1", "password", "james@yahoo.com", "james");		
		jill = new User("jill1", "password", "jill@yahoo.com", "jill");		
		judy = new User("judy1", "password", "judy@yahoo.com", "judy");		
		jake = new User("jake1", "password", "jake@yahoo.com", "jake");		
		janas = new User("janas1", "password", "janas@yahoo.com", "janas");		
		jimmy = new User("jimmy1", "password", "jimmy@yahoo.com", "jimmy");
		jeremy = new User("jeremy1", "password", "jeremy@yahoo.com", "jeremy");
		
		//Add Conferences to Conference List
		conferences.add(conf1);
		conferences.add(conf2);
		conferences.add(conf3);
		
		//Add Users to Userlist
		users.add(admin);	
		users.add(john);
		users.add(jack);
		users.add(jim);
		users.add(james);
		users.add(jill);
		users.add(judy);
		users.add(jake);
		users.add(janas);
		users.add(jimmy);

	}
	
	/**
	 * Test method that will make sure an admin account is seeded into the system
	 */
	@Test
	public void testNoSerialFileExistsAdminAccountSeeded() {
		
		//Test to be sure the admin account is not null
		assertNotNull("Admin account should not be Null", controller.getAdminUser());
		
		//Test to be sure the admin account is correct/the same
		assertEquals("Login not the same", controller.getAdminUser().getLogin(), admin.getLogin());
	}
	
	/**
	 * Test method that will add data to the user and conference list, save the data to file
	 * then load this data to be sure it loads correctly. A check on the expected User count
	 * and conference count is completed.
	 */
	@Test
	public void testSaveThenLoadDataCorrectly() {
		//lets add some Users and Conferences to the controller
		addUsersToController();
		addConferencesToController();
		
		//now save the files
		controller.saveData();
		
		//now load that data back into controller for testing
		controller.loadFile();
		
		//Test to see if the all users were loaded and saved into the list correctly (users + 1 admin account).
		assertEquals("User Count not the same", 11, controller.getUserListActualSize());
		
		//Test to see if the all conferences were loaded and saved into the list correctly (conf = 3).
		assertEquals("User Count not the same", 3, controller.getConferenceList().size());
		

	}
	
	/**
	 * Test method that will test the various User Data inside the User List
	 * to make sure the data was loaded correctly.
	 */
	@Test
	public void testUserLoadedData() {
		//Test various indexes and various data to be sure the users were loaded back correctly
		assertEquals("User login not the same", "jeremy1" , controller.getUserList().get(controller.getUserList().size() - 1).getLogin());		
		assertEquals("User name not the same", "jimmy" , controller.getUserList().get(controller.getUserList().size() - 2).getfullname());
		assertEquals("User email not the same", "janas@yahoo.com" , controller.getUserList().get(controller.getUserList().size() - 3).getEmail());
		assertEquals("User password not the same", "password" , controller.getUserList().get(controller.getUserList().size() - 5).getPassword());
		
		//Test the permission for the user to be sure they are correct and the same
		boolean[] permis = new boolean[]{false, false, false, false, false};
		assertEquals("User permissions 1 not the same", permis [0] , controller.getUserList().get(controller.getUserList().size() - 6).getPermissions() [0]);
		assertEquals("User permissions 2 not the same", permis [1] , controller.getUserList().get(controller.getUserList().size() - 6).getPermissions() [1]);
		assertEquals("User permissions 3 not the same", permis [2] , controller.getUserList().get(controller.getUserList().size() - 6).getPermissions() [2]);
		assertEquals("User permissions 4 not the same", permis [3] , controller.getUserList().get(controller.getUserList().size() - 6).getPermissions() [3]);
		assertEquals("User permissions 5 not the same", permis [4] , controller.getUserList().get(controller.getUserList().size() - 6).getPermissions() [4]);

	}
	
	/**
	 * Test method for {@link Model.Controller#getReviewerEmailAddresses()}.
	 */
	@Test
	public void testConferenceListCorrect() {
		//Test the list is not empty and correct size
		assertTrue("Conference list not empty", controller.getConferenceList().size() > 0);
		assertTrue("Conference list not empty", controller.getConferenceList().size() == 3);
		
		//Test conference name to be sure the conferences were loaded back correctly
		assertEquals("Conference 1 name not the same", "Conference 1", controller.getConferenceList().get(0).getConferenceName());
		assertEquals("Conference 2 name not the same", "Conference 2", controller.getConferenceList().get(1).getConferenceName());
		assertEquals("Conference 3 name not the same", "Conference 3", controller.getConferenceList().get(2).getConferenceName());
		
		assertEquals("Conference 1 deadline not the same", deadline1.getDateDay(), controller.getConferenceList().get(0).getNotifyAuthorsDeadline().getDateDay());
		assertEquals("Conference 2 deadline not the same", deadline2.getDateMonth(), controller.getConferenceList().get(1).getPaperRevisionSubmissionDeadline().getDateMonth());
		assertEquals("Conference 3 deadline not the same", deadline3.getDateYear(), controller.getConferenceList().get(2).getPaperSubmissionDeadLine().getDateYear());
		
		assertEquals("Conference 1 PChair not correct", john.getLogin(), controller.getConferenceList().get(0).getProgramChair().getLogin());
		assertEquals("Conference 2 PChair not correct", jack.getPassword(), controller.getConferenceList().get(1).getProgramChair().getPassword());
		assertEquals("Conference 3 PChair not correct", jim.getEmail(), controller.getConferenceList().get(2).getProgramChair().getEmail());

	}
	
	/**
	 * Test method that will empty the conference list and then save
	 * as to not include these fake conferences inside the conf_data.ser file.
	 * Also change the user perms to the old PC
	 */
	@Test
	public void testEmptyConferenceList() {
		conferences.remove(conf1);
		conferences.remove(conf2);
		conferences.remove(conf3);
        controller.removeConferencesFromSystem();
		controller.saveData();
	}
	
	/**
	 * Helper method to add Users to the controller for saving and loading
	 */
	private void addUsersToController() {
		controller.addUserToSystem(john);
		controller.addUserToSystem(jack);
		controller.addUserToSystem(jim);
		controller.addUserToSystem(james);
		controller.addUserToSystem(jill);
		controller.addUserToSystem(judy);
		controller.addUserToSystem(jake);
		controller.addUserToSystem(janas);
		controller.addUserToSystem(jimmy);
		controller.addUserToSystem(jeremy);
	}
	
	/**
	 * Helper method to add conferences to the controller for saving and loading
	 */
	private void addConferencesToController() {
		controller.addConferenceToSystem(conf1);
		controller.addConferenceToSystem(conf2);
		controller.addConferenceToSystem(conf3);
	}
	
	
	
} 